
import DistributionOrderItem from './components/OrderItem/index';
import UserList from './components/UserList/index';
import Api from '@/services/api';
import moment from 'moment';
// import { mapping } from '@/mixin/mixins/mapping';
export default {
  // mixins: [mapping],
  components: {
    DistributionOrderItem,
    UserList,
  },
  data() {
    return {
      info: {},  // 分销角色信息
      tabs: ['推广订单', '邀请订单', '用户列表'],
      distributionData: [{
        title: '今日收益',
      }, {
        title: '累计收益',
      }, {
        title: '可提现佣金',
        pageUrl: '/distribution/cashOut/index'
        // pageUrl: '/member/distribution/teamMembers/index'
      }, {
        title: '已提现佣金',
        pageUrl: '/distribution/cashOutRecord/index'
      }, {
        title: '团队佣金'
      }, {
        title: '团队成员',
      }],
      pagePayload: {
        pageNo: 0,
        pageSize: 10
      },
      distributionOrders: [],  // 分销中心订单
      inviteUsers: [],  // （邀请）用户列表
      activeTab: 0,
      qrCode: '', // 邀请二维码
    }
  },
  onLoad() {
  },
  onShow() {
    this.initData();
  },
  computed: {
    privacyData() {
      return this.$store.state.privacyData;
    }
  },
  mounted() {
  },
  methods: {
    initData() {
      this.getDistributionInfo();  // 获取分销信息
      // this.getDistributionOrderList();  // 获取分销订单
      // this.getAppletShareUserQr();
    },
    // 获取分销商信息
    async getDistributionInfo() {
      // 获取用户信息
      const {
        code,
        result
      } = await this.$request({
        url: Api.getAgentBasicInfor,
      })
      if (code === 200) {
        const extinfor = JSON.parse(result.extinfor || '{}');
        this.info = { ...result, ...extinfor };
      }
    },
    // 获取邀请二维码
    async getAppletShareUserQr() {
      const { suc, result } = await this.$request({
        url: Api.getAppletShareUserQr,
        data: {
          userId: this.privacyData.userId,
          pagesPath: 'member/teamInvitation/index'
        }
      })
      if (suc) {
        this.qrCode = result;
      }
    },
    handleTab(e) {
      const { index } = e.currentTarget.dataset;
      // 点击同一个tab不加载
      if (this.activeTab == index) return;
      this.activeTab = index;
      // if (this.activeTab == 3 && !this.inviteUsers.length) {
      //   this.getInviteUser();
      // } else {
      //   // 切换时需要把页码重置
      //   this.pagePayload.pageNo = 1;
      //   this.getDistributionOrderList()
      // }
    },
    // 获取分销中心订单
    async getDistributionOrderList() {
      const { suc, result } = await this.$request({
        url: Api.getDistributionOrder,
        method: 'GET',
        data: {
          userId: this.privacyData.userId,
          type: this.activeTab + 1,
          ...this.pagePayload
        }
      })
      if (suc) {
        result.orders.map(i => {
          i.iconImg = JSON.parse(i.iconImg || '[]');
        })
      }
    },
    // 获取邀请用户列表
    async getInviteUser() {
      const { suc, result } = await this.$request({
        url: Api.getInviteUser,
        method: 'GET',
        data: {
          userId: this.privacyData.userId,
          pageNo: 1,
          pageSize: 100
        }
      })
      if (suc) {
        result.map(i => {
          i.unBindTime = moment(i.unBindTime).format('YYYY-MM');
          i.inviteTime = moment(i.bindTime).format('YYYY-MM');
          return i;
        })
        this.inviteUsers = result;
      }
    },
    //分销数据点击事件
    onTapDistributionItem(e) {
      let { pageUrl, index } = e.currentTarget.dataset;
      switch (index) {
        case 2:
          pageUrl += `?maxAmount=${this.info.canWithdrawnCommission}`
          break;

        default:
          break;
      }
      uni.navigateTo({
        url: pageUrl
      })
    },
    // 点击海报分享
    handleShare() {
      this.$refs.posterModal.toggle(true);
    }
  },
  onShareAppMessage(res) {
    // 返回分享的内容
    return {
      title: '侣途等你加入',
      content: '加入侣途',
      path: '/member/teamInvitation/index',
      imageUrl: 'https://ltcx-img.oss-cn-beijing.aliyuncs.com/miniprogram/common/income.png' // 分享卡片的图片，可选
    };
  },
}